package libraryRun;

import mySQLServer.Link;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Logging extends JFrame {
    public Logging(){
        super("登录界面");

    }
    public void loggingMethod(Logging logging,MainFrame myJFrame,JTextField jTextField){
        setLayout(null);
        Container container=this.getContentPane();
        setSize(1200,650);

        //面板
        JPanel p1=new JPanel(new FlowLayout(FlowLayout.CENTER,0,35));
        JPanel p2=new JPanel(new FlowLayout(FlowLayout.CENTER,20,40));

        //标签
        JLabel label1=new JLabel("用 户 登 录");
        JLabel label2=new JLabel("用 户 名");
        JLabel label3=new JLabel("用 户 Id");
        JLabel label4=new JLabel("密 码");

        //文本行
        JTextField field1=new JTextField(15);
        JTextField field2=new JTextField(15);

        //密码框
        JPasswordField password1=new JPasswordField(15);

        //按钮
        JButton button1=new JButton("登 录");
        JButton button2=new JButton("取 消");

        //监听器
        ImageIcon imageIcon1=new ImageIcon("icon/imageIcon6.jpg");
        imageIcon1.setImage(imageIcon1.getImage().getScaledInstance(50,45,Image.SCALE_AREA_AVERAGING));
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {  //参数“int optionType”确定了按钮的定制
                if(JOptionPane.showConfirmDialog(logging,"你确定吗？","确认登录",JOptionPane.YES_NO_CANCEL_OPTION,1,imageIcon1)==0){
                    String s=new String(password1.getPassword());
                    if(field1.getText().equals("")||field2.getText().equals("")){
                        JOptionPane.showMessageDialog(logging,"请完成登录","登录提示",JOptionPane.INFORMATION_MESSAGE,imageIcon1);
                        return;
                    }
                    Link link=new Link();
                    try{
                        Connection con=link.linkDatebase();
                        if(loggingUser(con,field1.getText(),field2.getText(),s)==3){
                            jTextField.setText("ID:"+field2.getText());
                            jTextField.setHorizontalAlignment(JTextField.CENTER);
                            myJFrame.setUserName(field1.getText());
                            myJFrame.setUserId(field2.getText());
                            myJFrame.setLogged(true);
                        }
                        else if(loggingUser(con,field1.getText(),field2.getText(),s)==1){
                            JOptionPane.showMessageDialog(logging,"用户名和Id不匹配，登录失败！","登录提示",JOptionPane.INFORMATION_MESSAGE,imageIcon1);
                            return;
                        }
                        else if(loggingUser(con,field1.getText(),field2.getText(),s)==2){
                            JOptionPane.showMessageDialog(logging,"密码错误，登录失败！","登录提示",1,imageIcon1);
                            return;
                        }
                        else{
                            JOptionPane.showMessageDialog(logging,"用户Id不存在，请注册！","登录提示",1,imageIcon1);
                            return;
                        }
                        link.closeDatebase(con);
                        myJFrame.setVisible(true);
                        dispose();

                    }catch (ClassNotFoundException e1){
                        e1.printStackTrace();
                    }catch (SQLException e1){
                        e1.printStackTrace();
                    }
                }
            }
        });

        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                myJFrame.setVisible(true);
                dispose();
            }
        });

        addWindowListener(new WindowListener() {
            public void windowOpened(WindowEvent e) {

            }

            public void windowClosing(WindowEvent e) {
                myJFrame.setVisible(true);
            }

            public void windowClosed(WindowEvent e) {

            }

            public void windowIconified(WindowEvent e) {

            }

            public void windowDeiconified(WindowEvent e) {

            }

            public void windowActivated(WindowEvent e) {

            }


            public void windowDeactivated(WindowEvent e) {

            }
        });

        //字体
        Font font=new Font("宋体",Font.BOLD,45);
        Font font1=new Font("宋体",Font.BOLD,30);
        Font font2=new Font("宋体",Font.BOLD,25);


        //设置字体
        label1.setFont(font);
        label2.setFont(font1);
        label3.setFont(font1);
        label4.setFont(font1);
        field1.setFont(font2);
        field2.setFont(font2);
        password1.setFont(font2);


        //设置尺寸
        label2.setPreferredSize(new Dimension(180,30));
        label3.setPreferredSize(new Dimension(180,30));
        label4.setPreferredSize(new Dimension(180,30));
        button1.setPreferredSize(new Dimension(100,30));
        button2.setPreferredSize(new Dimension(100,30));

        //设置位置
        p1.setBounds(250,30,700,120);
        p2.setBounds(340,170,500,350);

        //放入面板
        p1.add(label1);

        p2.add(label2);
        p2.add(field1);
        p2.add(label3);
        p2.add(field2);
        p2.add(label4);
        p2.add(password1);
        p2.add(button1);
        p2.add(button2);


        //放入容器
        container.add(p1);
        container.add(p2);

        setIconImage((new ImageIcon("icon/imageIcon5.jpg")).getImage());
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
    }
    public int loggingUser(Connection con,String name,String Id,String password) throws SQLException {
        int isExist=0,isUserflag=0,isPasswordflag=0;//用户名和Id一致  密码无误
        String password1=null;
        String sql="select userName from [user] where userId='"+Id+"'";
        String sql1="select userPassword from [user] where userId='"+Id+"'";
        Statement stmt=con.createStatement();
        ResultSet rs=stmt.executeQuery(sql);
        while(rs.next()){
            isExist=1;
            if(name.equals(rs.getString("userName")))
                isUserflag=1;
        }

        rs=stmt.executeQuery(sql1);
        while(rs.next()){
            if(password.equals(rs.getString("userPassword")))
                isPasswordflag=1;
        }
        if(isExist==1){
            if(isUserflag==1){
                if(isPasswordflag==1)
                    return 3;
                else
                    return 2;
            }
            else
                return 1;
        }
        else
            return 0;
    }

}
